<?php
class CheckLog {
	private $domain;
	private $ticket;
	private $sign;
	private $checksum;
	private $private_key;
	private $result;
	private $sec;
	private $usec;
	
	public function CheckLog() {
		$this->domain   = daddslashes($_GET['domain']);
		$this->sign     = daddslashes($_GET['ticket']);
		$this->ticket   = substr($this->sign,0,-32);
		$this->checksum = substr($this->sign,-32,32);
	}
	
	private function selectClient() {
		$selectCountClientSql = "SELECT count( private_key ) as count_private_key
		                           FROM `client`
		                          WHERE `domain` = '".$this->domain."'";
		$resultCount = $GLOBALS['FENGTOPssoDB']->GetOne($selectCountClientSql);
		if ($resultCount['count_private_key'] == 0) {
			$retunMsg['s'] = "2";
			$retunMsg['m'] = "signature invalid";
			$retunMsg['d'] = $GLOBALS['_FENGTOPsso']['config']['longinUrl'];
			exit;
		}
		
		$selectClientSql = "SELECT `private_key`
		                      FROM `client`
		                     WHERE `domain` = '".$this->domain."'";
		$result = $GLOBALS['FENGTOPssoDB']->GetOne($selectClientSql);
		$this->private_key = $result['private_key'];
	}
	
	public function checkUserLogin() {
		$this->selectClient();
		if ($this->checksum == md5($this->ticket.$this->private_key)) {
			$this->getOnlineUser();
		}else {
			$this->result = "用户未登录！";
		}
	}
	
	private function getOnlineUser() {
		$selectOnlineUser = "SELECT *
		                       FROM `onlineuser`
		                      WHERE `ticket` = '$this->ticket'";
		$this->result = $GLOBALS['FENGTOPssoDB']->GetOne($selectOnlineUser);
	}
	
	public function coutResult() {
		$retunMsg;
		if( ($this->sec - $this->result['expiry']) > $GLOBALS['_FENGTOPsso']['config']['maxTime'] )	{
			$retunMsg['s'] = "1";
			$retunMsg['m'] = "relogin";
			$retunMsg['d'] = $GLOBALS['_FENGTOPsso']['config']['longinUrl'];
		}else{
			$retunMsg['s'] = "0";
			$retunMsg['m'] = "success";
			$retunMsg['d'] = $this->ticket;
		}
		echo json_encode($retunMsg);
	}
	
	private function getStartTime() {
		list ( $this->usec, $this->sec ) = explode ( " ", microtime () );
	}
	
}

?>

